package com.jhd.fuzhou.view.daily;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.TextDialog;
import com.jhd.fuzhou.util.span.ReplaceSpan;
import com.jhd.fuzhou.util.span.SpansManager;
import com.jhd.fuzhou.vo.IllegalRecordDtlVo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

/*填写违法行为的规范用语*/
public class DailyStandardWordActivity extends BaseActivity implements ReplaceSpan.OnClickListener{
    @BindView(R.id.btn_continue_add)Button btn_continue_add;
    @BindView(R.id.btn_finish)Button btn_finish;
    @BindView(R.id.tv_enforceBasis_name)TextView tv_enforceBasis_name;
    @BindView(R.id.tv_illegal_content)TextView tv_illegal_content;//违法行为
    @BindView(R.id.et_illegal_content)EditText et_illegal_content;
    @BindView(R.id.tv_supervision_opinions)TextView tv_supervision_opinions;//监督意见
    @BindView(R.id.et_supervision_opinions)EditText et_supervision_opinions;
    @BindView(R.id.iv_qualitative_basis)ImageView iv_qualitative_basis;//定性依据
    @BindView(R.id.tv_qualitative_basis)TextView tv_qualitative_basis;
    @BindView(R.id.iv_treatment_basis)ImageView iv_treatment_basis;//处理依据
    @BindView(R.id.tv_treatment_basis)TextView tv_treatment_basis;
    private Context context= DailyStandardWordActivity.this;
    private IllegalRecordDtlVo illegalRecordDtlVo;
    private SpansManager illegalSpansManager,supervisionSpansManager;
    private String illegalContent,supervisionOpinions;
    private boolean qualitative=false;
    private boolean supervision=false;
    private List<IllegalRecordDtlVo> illegalRecordDtlVoVoList;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_standard_word);
    }

    @Override
    protected void processLogic() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        illegalRecordDtlVo = JSON.parseObject(getIntent().getStringExtra("enforceBasis"), IllegalRecordDtlVo.class);
        show();
        if(illegalContent!=null){
            illegalSpansManager = new SpansManager(this,tv_illegal_content,et_illegal_content,0);
            illegalSpansManager.doFillBlank(illegalContent);
        }
        if(supervisionOpinions!=null){
            supervisionSpansManager = new SpansManager(this,tv_supervision_opinions,et_supervision_opinions,1);
            supervisionSpansManager.doFillBlank(supervisionOpinions);
        }
    }
    public void show(){
        tv_enforceBasis_name.setText(illegalRecordDtlVo.getContent());
        illegalContent= illegalRecordDtlVo.getIllegalBehavior();
        supervisionOpinions= illegalRecordDtlVo.getSuperviseOpinion();
        tv_qualitative_basis.setText(illegalRecordDtlVo.getQualitativeBasis());//定性依据
        tv_qualitative_basis.setMaxLines(3);
        tv_treatment_basis.setText(illegalRecordDtlVo.getHandleBasis());//处理依据
        tv_treatment_basis.setMaxLines(3);
    }
    @OnClick({R.id.btn_continue_add,R.id.btn_finish,R.id.iv_qualitative_basis,R.id.iv_treatment_basis})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_continue_add:
                startActivity(new Intent(context, DailyCheckContentActivity.class));
                break;
            case R.id.btn_finish:
                if(supervisionInspectionVo.getIllegalRecordDtlVoList()==null){
                    illegalRecordDtlVoVoList =new ArrayList<>();
                    illegalRecordDtlVo.setIllegalBehavior(getResult(illegalSpansManager,et_illegal_content,illegalContent).trim());
                    illegalRecordDtlVo.setSuperviseOpinion(getResult(supervisionSpansManager,et_supervision_opinions,supervisionOpinions).trim());
                    illegalRecordDtlVo.setModifyTime(null);
                    illegalRecordDtlVo.setModifyUser(null);
                    illegalRecordDtlVoVoList.add(0, illegalRecordDtlVo);
                    supervisionInspectionVo.setIllegalRecordDtlVoList(illegalRecordDtlVoVoList);
                }else{
                    illegalRecordDtlVoVoList = supervisionInspectionVo.getIllegalRecordDtlVoList();
                    for (IllegalRecordDtlVo basis : illegalRecordDtlVoVoList) {
                        if(basis.getId().equals(illegalRecordDtlVo.getId())){
                            illegalRecordDtlVoVoList.remove(basis);
                            break;
                        }
                    }
                    illegalRecordDtlVo.setIllegalBehavior(getResult(illegalSpansManager,et_illegal_content,illegalContent).trim());
                    illegalRecordDtlVo.setSuperviseOpinion(getResult(supervisionSpansManager,et_supervision_opinions,supervisionOpinions).trim());
                    illegalRecordDtlVo.setModifyTime(null);
                    illegalRecordDtlVo.setModifyUser(null);
                    illegalRecordDtlVoVoList.add(0, illegalRecordDtlVo);
                    supervisionInspectionVo.setIllegalRecordDtlVoList(illegalRecordDtlVoVoList);
                }
                finish();
                break;
            case R.id.iv_qualitative_basis://定性依据
                if(qualitative){
                    iv_qualitative_basis.setImageResource(R.mipmap.ic_down);
                    tv_qualitative_basis.setMaxLines(3);
                    qualitative=false;
                }else{
                    iv_qualitative_basis.setImageResource(R.mipmap.ic_go);
                    tv_qualitative_basis.setMaxLines(10000);
                    qualitative=true;
                }
                break;
            case R.id.iv_treatment_basis://处理依据
                if(supervision){
                    iv_treatment_basis.setImageResource(R.mipmap.ic_down);
                    tv_treatment_basis.setMaxLines(3);
                    supervision=false;
                }else{
                    iv_treatment_basis.setImageResource(R.mipmap.ic_go);
                    tv_treatment_basis.setMaxLines(10000);
                    supervision=true;
                }
                break;
        }
    }
    //填空题点击响应事件
    @Override
    public void OnClick(TextView v, int id, ReplaceSpan span,int type) {
        if (type == 0) {
            TextDialog.spanDialog(context,"请输入",et_illegal_content,id,span,illegalSpansManager);
        }else{
            TextDialog.spanDialog(context,"请输入",et_supervision_opinions,id,span,supervisionSpansManager);
        }
        //如果当前span身上有值，先赋值给et身上
    }

    private String getResult(SpansManager spansManager,EditText editText,String content){
        spansManager.setLastCheckedSpanText(editText.getText().toString());
        return spansManager.getResult(content);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}